//
//  GKPullDownRefreshView.swift
//  MicroBlogFunc
//
//  Created by GK on 16/4/16.
//  Copyright © 2016年 GK. All rights reserved.
//

import UIKit

//下拉刷新view的高度
private let refreshViewH: CGFloat = 60
// 下拉刷新三种状态的枚举
enum GKPullDownRefreshViewType : Int {
    
    case Normal //下拉箭头向下
    case ReleaseToRefresh //下拉箭头向上,提示松手刷新
    case Refreshing //松手时 刷新转 菊花
}

private let keyPath = "contentOffset"


class GKPullDownRefreshView: UIView {

    private var superScrollView: UIScrollView?

    //默认箭头向下模式
    private var status: GKPullDownRefreshViewType = .Normal
    
    override init(frame: CGRect) {
        let newFrame = CGRect(x: 0, y: -refreshViewH, width: screenW, height: refreshViewH)
        super.init(frame: newFrame)
        backgroundColor = UIColor.redColor()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //当控价被添加到父控件的时候会调用此方法,此方法就可以拿到父控件
    override func willMoveToSuperview(newSuperview: UIView?) {
        
        super.willMoveToSuperview(newSuperview)
        
        if let scrollView = newSuperview as? UIScrollView
        {
            superScrollView = scrollView
            superScrollView?.addObserver(self, forKeyPath: keyPath, options: [], context: nil)
        }
        
    }
    
    //当 对象被销毁的时候会调用此方法 , 此时移除监听
    deinit{
        removeObserver(self, forKeyPath: keyPath)
    }
    
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<Void>) {
        GKPrint(" tableview y坐标的偏移量\(superScrollView?.contentOffset.y)")
    }
    
}
